package com.jn.zhinongbao.adapter.bean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 聊天消息实体类
 */
public class ChatMessage {
    private String id; // 消息ID
    private String chatRoomId; // 聊天室ID
    private int senderId; // 发送者ID
    private String senderName; // 发送者名称
    private String content; // 消息内容
    private int messageType; // 消息类型: 0-文本，1-图片，2-语音
    private String sendTime; // 发送时间
    private boolean isRead; // 是否已读
    private boolean isSentByMe; // 是否是我发送的

    // 消息类型常量
    public static final int TYPE_TEXT = 0;
    public static final int TYPE_IMAGE = 1;
    public static final int TYPE_VOICE = 2;

    public ChatMessage() {
        this.sendTime = getCurrentTimeString();
        this.isRead = false;
        this.messageType = TYPE_TEXT;
    }

    public ChatMessage(String chatRoomId, int senderId, String senderName, String content) {
        this.chatRoomId = chatRoomId;
        this.senderId = senderId;
        this.senderName = senderName;
        this.content = content;
        this.sendTime = getCurrentTimeString();
        this.isRead = false;
        this.messageType = TYPE_TEXT;
        this.id = generateMessageId();
    }

    private String getCurrentTimeString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }

    private String generateMessageId() {
        return chatRoomId + "_" + System.currentTimeMillis();
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getChatRoomId() {
        return chatRoomId;
    }

    public void setChatRoomId(String chatRoomId) {
        this.chatRoomId = chatRoomId;
    }

    public int getSenderId() {
        return senderId;
    }

    public void setSenderId(int senderId) {
        this.senderId = senderId;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getMessageType() {
        return messageType;
    }

    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }

    public String getSendTime() {
        return sendTime;
    }

    public void setSendTime(String sendTime) {
        this.sendTime = sendTime;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public boolean isSentByMe() {
        return isSentByMe;
    }

    public void setSentByMe(boolean sentByMe) {
        isSentByMe = sentByMe;
    }

    /**
     * 获取格式化的发送时间（仅显示时分）
     * @return 格式化的时间字符串
     */
    public String getFormattedTime() {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            Date date = inputFormat.parse(sendTime);
            return outputFormat.format(date);
        } catch (Exception e) {
            return sendTime;
        }
    }
} 